----------------------------------Spell Variables--------------------------------------
SPELL.m_iSpellID = 70201 -- Whatever value you want as long as it doesn't override any other spells' IDs.
SPELL.m_iRange = 1024
SPELL.m_sName = "Powder Bomb"
SPELL.m_sToolTip =	[[Throw a bomb at the
					target, dealing 15 damage
					to all targets nearby.]]
SPELL.m_sIcon = "devin/blackbetty/spells/ranger/powderbomb.jpg"
SPELL.m_iCoolDown = 12
SPELL.m_iManaCost = 50
SPELL.m_bRequiresTarget = true
SPELL.m_iSpellType = SPELLTYPE_RANGEDAMAGE

---------------------------------------------------------------------------------------

function SPELL:Cast( pl, target )
	timer.Simple(0.8, function()
		pl:SetNWBool("Casting", false) 
		pl:SetNWBool("Casted", false)
	end)

	local range = self.m_iRange
	
	print("Casted "..self.m_sName.."!")
		
	if(target && target:IsValid() && target != pl) then
		if(pl:GetPos():Distance(target:GetPos()) < range)then
			if(pl:IsPlayer())then
				pl:DoAnimationEvent(ACT_GMOD_GESTURE_ITEM_THROW)
			else
				pl:RestartGesture(ACT_HL2MP_GESTURE_RANGE_ATTACK_MELEE)
			end
			timer.Simple(0.65, function()
				local bombRadius = ents.FindInSphere(target:GetPos(), 240)
				if(SERVER)then
					pl:EmitSound("weapons/iceaxe/iceaxe_swing1.wav", 100, 50+math.random(-10,10))
					local myhitdelay = ((pl:GetPos() - target:GetPos()):Length()/1400)
					LaunchProjectile(pl, target, 0, "pummel_hit", "", "bombtrail", "devin/blackbetty/weapons/travelsounds/FireMissileLoop.wav", myhitdelay, 800, "models/props_junk/Rock001a.mdl")
					timer.Simple(myhitdelay, function()
						ParticleEffect( "bomb_explosion", target:GetPos(), Angle(0,0,0), target )
						pl:EmitSound("devin/blackbetty/weapons/hit/Fire"..math.random(1,3)..".wav", 40, 100+math.random(-10,10))
						for i, s in pairs(bombRadius)do
							if(s and s:EntIndex() != pl:EntIndex() and s:IsPlayer() or s:IsNPC() or s:GetClass() == "npc_rpgdm")then
								s:EmitSound("devin/blackbetty/weapons/Warrior_ShockWave_Area.wav", 150, 100+math.random(30,60))
								ParticleEffect( "pummel_hit", s:GetPos() + Vector(0, 0, 46) + (s:GetAngles():Forward()*6), Angle(0,0,0), pl )
								ParticleEffect( "player_blood", s:GetPos() + Vector(0, 0, 46) + (s:GetAngles():Forward()*6), Angle(0,0,0), pl )
								if SERVER then
									s.Speed = true 
									s.SpeedMod = 0.35
									s.SpeedModDuration = 6
									s.SpeedType = "Charge"
									timer.Simple(0.2, function() 
										local dmginfo = DamageInfo()
											dmginfo:SetDamage( 15 ) 
											dmginfo:SetDamageType( DMG_BURN ) 
											dmginfo:SetAttacker( pl )
										s:TakeDamageInfo( dmginfo ) 
									end)
								end	
							end
						end
						target:EmitSound("weapons/mortar/mortar_explode1.wav", 100, 100+math.random(-10,10))
						ParticleEffect( "impact_firebig", target:GetPos() + Vector(0, 0, 32), Angle(0,0,0), pl )
						ParticleEffect( "pummel_hit", target:GetPos() + Vector(0, 0, 45), Angle(0,0,0), target )
					end)
				end
			end)
		end
	end
end




